"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var iview_1 = require("iview");
function reduceHandleEven(func) {
    if (!func) {
        return;
    }
    else if (Object.prototype.toString.call(func) === '[object Function]') {
        func && func();
    }
    else if (func.constructor === Array || Object.prototype.toString.call(func) === '[object Array]') {
        func.map(function (item) {
            if (typeof item === 'function') {
                item();
            }
        });
    }
}
var MessagePlugin = {
    install: function (Vue, options) {
        Vue.prototype.$MessagePlugin = {
            info: function (message, duration, closable) {
                if (duration === void 0) { duration = 4; }
                iview_1.Message.info({ content: message, duration: duration || 4, closable: closable || false });
            },
            success: function (message, duration, closable) {
                if (duration === void 0) { duration = 4; }
                iview_1.Message.success({ content: message, duration: duration || 2, closable: closable || false });
            },
            warn: function (message, duration, closable) {
                if (duration === void 0) { duration = 4; }
                iview_1.Message.warning({ content: message, duration: duration || 4, closable: closable || false });
            },
            fail: function (message, duration, closable) {
                if (duration === void 0) { duration = 4; }
                iview_1.Message.error({ content: message, duration: duration || 4, closable: closable || false });
            }
        };
        Vue.prototype.AutoMessage = function (option) {
            option = option || {};
            option.message = option.message || 'success';
            option.error = option.error || 'fail';
            option.state = option.state || null;
            option.even = option.even || [];
            if (option.state && option.state.isResolved) {
                var state = option.state.data;
                var operationState = state && state.success;
                if (operationState) {
                    this.$MessagePlugin.success(option.message || state.message);
                    reduceHandleEven(option.even);
                }
                else {
                    this.$MessagePlugin.fail(state.message || option.error);
                }
            }
        };
        Vue.prototype.messageAlert = function (curVal, message, failMessage, funcArray) {
            if (failMessage && failMessage.constructor === Array) {
                funcArray = failMessage;
            }
            if (message && message.constructor === Array) {
                funcArray = message;
            }
            if (curVal.isResolved) {
                var result = curVal.data.success;
                if (result) {
                    this.$MessagePlugin.success(message || curVal.data.message);
                    reduceHandleEven(funcArray);
                }
                else {
                    this.$MessagePlugin.fail(curVal.data.message || failMessage);
                }
            }
        };
    }
};
exports.default = MessagePlugin;
